#ifndef __TIMER_H__
#define __TIMER_H__

#include <asm/type.h>

/*
 * Bits         Usage
 * 6 and 7      Select channel :
 *                 0 0 = Channel 0
 *                 0 1 = Channel 1
 *                 1 0 = Channel 2
 *                 1 1 = Read-back command (8254 only)
 * 4 and 5      Access mode :
 *                 0 0 = Latch count value command
 *                 0 1 = Access mode: lobyte only
 *                 1 0 = Access mode: hibyte only
 *                 1 1 = Access mode: lobyte/hibyte
 * 1 to 3       Operating mode :
 *                 0 0 0 = Mode 0 (interrupt on terminal count)
 *                 0 0 1 = Mode 1 (hardware re-triggerable one-shot)
 *                 0 1 0 = Mode 2 (rate generator)
 *                 0 1 1 = Mode 3 (square wave generator)
 *                 1 0 0 = Mode 4 (software triggered strobe)
 *                 1 0 1 = Mode 5 (hardware triggered strobe)
 *                 1 1 0 = Mode 2 (rate generator, same as 010b)
 *                 1 1 1 = Mode 3 (square wave generator, same as 011b)
 * 0            BCD/Binary mode: 0 = 16-bit binary, 1 = four-digit BCD
 */

#define GET_FREQ(r) (119380 / (r))
#define FREQ_100MHZ (119380 / 100)
#define TIMER_CMD_PORT 0x43   // 模式/命令寄存器（仅写入，忽略读取）
#define TIMER0_DATA_PORT 0x40 // 通道0 数据端口（读/写）
#define TIMER1_DATA_PORT 0x41 // 通道1 数据端口（读/写）
#define TIMER2_DATA_PORT 0x42 // 通道2 数据端口（读/写）
#define CMD_CHANNEL_0 0x00
#define CMD_CHANNEL_1 0x40
#define CMD_CHANNEL_2 0x80
#define CMD_SAVE_CPU 0x00
#define CMD_RDWR_LOW 0x10
#define CMD_RDWR_HIGH 0x20
#define CMD_RDWR_ALL 0x30
#define CMD_MODE_0 0x00
#define CMD_MODE_1 0x02
#define CMD_MODE_2 0x04
#define CMD_MODE_3 0x06
#define CMD_MODE_4 0x08
#define CMD_MODE_5 0x0a
#define CMD_FORMAT_BINARY 0x00
#define CMD_FORMAT_BCD 0x01

extern void init_timer(u32_t freq);

#endif //__TIMER_H__